#!/bin/bash
wget https://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/LinuxVMDataDiskAutoInitialize.sh
chmod -x LinuxVMDataDiskAutoInitialize.sh
yum -y install expect
/usr/bin/expect <<EOF
spawn sh LinuxVMDataDiskAutoInitialize.sh
expect "Step 3: Please choose the disk"
send "/dev/vdb\r"
expect "Please enter a location to mount"
send "/data-disk\r"
expect eof
exit
EOF
cd /data-disk
yum -y install gcc gcc-c++ make
wget http://download.redis.io/releases/redis-5.0.8.tar.gz
tar -zxvf redis-5.0.8.tar.gz
cd redis-5.0.8
make
sed -i "s/bind 127.0.0.1/bind 0.0.0.0/g" /data-disk/redis-5.0.8/redis.conf
sed -i "s/daemonize no/daemonize yes/g" /data-disk/redis-5.0.8/redis.conf

sed -i 's!logfile ""!logfile "/var/log/redis_7000.log"!g' /data-disk/redis-5.0.8/redis.conf
sed -i "s!# requirepass foobared!requirepass $1!g" /data-disk/redis-5.0.8/redis.conf
sed -i "s/protected-mode yes/protected-mode no/g" /data-disk/redis-5.0.8/redis.conf
sed -i "s/port 6379/port 7000/g" /data-disk/redis-5.0.8/redis.conf
sed -i "s!pidfile /var/run/redis_6379.pid!pidfile /var/run/redis_7000.pid!g" /data-disk/redis-5.0.8/redis.conf
sed -i "s!dir ./!dir /data-disk/redis-5.0.8/tmp!g" /data-disk/redis-5.0.8/redis.conf
sed -i "s!# masterauth <master-password>!masterauth $1!g" /data-disk/redis-5.0.8/redis.conf
sed -i "s!# replicaof <masterip> <masterport>!replicaof 172.16.0.30 7000!g" /data-disk/redis-5.0.8/redis.conf

sed -i "s/port 26379/port 2700/g" /data-disk/redis-5.0.8/sentinel.conf
sed -i "s/daemonize no/daemonize yes/g" /data-disk/redis-5.0.8/sentinel.conf
sed -i 's!logfile ""!logfile "/var/log/redis-sentinel.log"!g' /data-disk/redis-5.0.8/sentinel.conf
sed -i "s!dir /tmp!dir /data-disk/redis-5.0.8/tmp!g" /data-disk/redis-5.0.8/sentinel.conf
sed -i 's!sentinel monitor mymaster 127.0.0.1 6379 2!sentinel monitor redis01 172.16.0.30 7000 2!g' /data-disk/redis-5.0.8/sentinel.conf
sed -i "s/sentinel down-after-milliseconds mymaster 30000/sentinel down-after-milliseconds redis01 10000/g" /data-disk/redis-5.0.8/sentinel.conf
sed -i "s/sentinel failover-timeout mymaster 180000/sentinel failover-timeout redis01 60000/g" /data-disk/redis-5.0.8/sentinel.conf
sed -i "s/sentinel parallel-syncs mymaster 1/sentinel parallel-syncs redis01 2 /g" /data-disk/redis-5.0.8/sentinel.conf
sed -i "s/# sentinel auth-pass <master-name> <password>/sentinel auth-pass redis01 $1/g" /data-disk/redis-5.0.8/sentinel.conf

ln -s /data-disk/redis-5.0.8/src/redis-server /usr/bin/redis-server
ln -s /data-disk/redis-5.0.8/src/redis-cli /usr/bin/redis-cli
ln -s /data-disk/redis-5.0.8/src/redis-sentinel /usr/bin/redis-sentinel

mkdir tmp

{
  echo  "172.16.0.30    $2-primary"
  echo  "172.16.0.31    $2-secondary01"
  echo  "172.16.0.32    $2-secondary02"
} >>/etc/hosts

redis-server /data-disk/redis-5.0.8/redis.conf
redis-sentinel /data-disk/redis-5.0.8/sentinel.conf

